<?php


function list_jadwal_kelulusan_pelatihan(){
	$header = array(  
		'Mulai'=>array('style'=>'border-bottom:2px solid;width:15%'),    
		'Nama pelatihan'=>array('style'=>'border-bottom:2px solid;width:45%'),    
		'Lulus'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'Gagal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'Absen'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'Null'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),
	);

	$query = "SELECT * FROM jadwal_pelatihan a
		INNER JOIN kategori_pelatihan b
			ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
		INNER JOIN materi_pelatihan c 
			ON c.materi_pelatihan_id=a.materi_pelatihan_id 
		ORDER BY a.start_date DESC 	
	 ";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=hasil_pelatihan&task=detail_peserta&jid='.$ey['jadwal_pelatihan_id'],
						 
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_props.png' , $editproperty  );
		$lulus = count_peserta_hasil($ey['jadwal_pelatihan_id'],'lulus');
		$gagal = count_peserta_hasil($ey['jadwal_pelatihan_id'],'gagal');
		$absen = count_peserta_hasil($ey['jadwal_pelatihan_id'],'absen');
		$kosong = count_peserta_($ey['jadwal_pelatihan_id']) - ($lulus + $gagal + $absen) ;
		$row[] = array(
			'l'=>  date("d-m-Y" , strtotime($ey['start_date']))  ,    
			'ms'=>  $ey['nama_pelatihan']  , 
			's'	=> position_text_align( $lulus,'center'),		
			'ss'	=> position_text_align( $gagal ,'center'),		
			'sda'	=>position_text_align(  $absen ,'center'),		
			'ssda'	=> position_text_align( $kosong ,	'center'),	
			'operasi'=> position_text_align(  $edit_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();	
	return  table_builder($header , $datas ,  8 , false ,$paging);
}

function get_jumlah_peserta_pelatihan($jadwal_pelatihan_id){
	$query = "SELECT COUNT(*) AS total_peserta FROM karyawan_pelatihan 
		WHERE jadwal_pelatihan_id = {$jadwal_pelatihan_id}";
	$result = my_query($query);
	$rw = my_fetch_array($result);
	return $rw['total_peserta'];
}

function hasil_peserta_jadwal_page($jadwal_pelatihan_id){

	$jadwal = my_get_data_by_id('jadwal_pelatihan','jadwal_pelatihan_id',$jadwal_pelatihan_id);
	$materi_pelatihan_id = $jadwal['materi_pelatihan_id'];
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),  
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Hasil'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Keterangan'=>array('style'=>'text-align:center;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	); 
	$query = "
		SELECT * FROM  karyawan_pelatihan  WHERE  jadwal_pelatihan_id ={$jadwal_pelatihan_id}
 ";
 
	$result = my_query($query);
	$row = array();
	while($ey = my_fetch_array($result)){
		$i++; 
		 
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$detailproperty = array(
						'href'=>'index.php?com=peserta_pelatihan&task=detail&kid='.$ey['materi_pelatihan_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$f = is_karyawan_selected($ey['karyawan_id'] ,$jadwal_pelatihan_id);
		if( is_karyawan_materi_selected($ey['karyawan_id'],$materi_pelatihan_id) ){
			if(!$f)
			continue;
		}
		$hasil_penilaian =get_hasil($ey['karyawan_id'] ,$jadwal_pelatihan_id ); 
		 $editproperty = array(
						'href'=>'index.php?com=hasil_pelatihan&task=editjadwal_pelatihan&jid='.$jadwal_pelatihan_id .'&kid='.$ey['karyawan_id'],
						 'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
	 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>    position_text_align($karyawan['nik'],'center') ,  
			'nama'=>   $karyawan['nama'],     
			'hasil'=>   position_text_align( $hasil_penilaian['hasil'],'center') ,     
			'keterangan'=>$hasil_penilaian['keterangan'],     
			'operasi'=> position_text_align(  $edit_button , 'right'),  
		);
	}	
	$datas = table_rows($row);  
	
		$materi_pelatihan = my_get_data_by_id('materi_pelatihan','materi_pelatihan_id' ,$materi_pelatihan_id); 
		$kategori_pelatihan = my_get_data_by_id('kategori_pelatihan','kategori_pelatihan_id' ,$materi_pelatihan['kategori_pelatihan_id']); 

	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "detail pegawai" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;"> 
	<!-- input type="button" value=" CETAK " class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=personal_resume&id='.$pelatihan_id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/ --> 
	<input type="button" value=" BACK " class="main_button" onclick="javascript:location.href=\'index.php?com=hasil_pelatihan\'"/></div></td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.strtoupper($kategori_pelatihan['kategori_pelatihan_label']).'</span>'   , "<b>Kategori pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($materi_pelatihan['materi_pelatihan_label']).'</span>'   , "<b>Materi pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($jadwal['nama_pelatihan']).'</span>'   , "<b>Nama pelatihan</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'. date('d - m - Y' , strtotime($jadwal['start_date']) ) .' s/d '. date('d - m - Y' , strtotime($jadwal['end_date']) ) .'</span>'   , "<b>Waktu pelatihan</b>"    ); 
	$view .= '</table>';
	$view .= '</td></tr></table><br/>'; 
	return  $view .  table_builder($header , $datas ,  5 , false ).'</form>';
}


function count_peserta_($pelatihan_id){
	$query = "
	SELECT COUNT(*) AS td FROM karyawan_pelatihan 
	WHERE jadwal_pelatihan_id={$pelatihan_id} 
	";
	$result = my_query($query);
	$rw = my_fetch_array($result);
	return  $rw['td'];
}

function count_peserta_hasil($pelatihan_id,$hasil){
$query = "
	SELECT COUNT(*) AS td FROM karyawan_pelatihan 
	WHERE hasil='{$hasil}' AND jadwal_pelatihan_id={$pelatihan_id} 
	";
	$result = my_query($query);
	$rw = my_fetch_array($result);
	return  $rw['td'];
}

function get_hasil($karyawan_id , $jadwal_pelatihan_id  ){
	$query = "SELECT hasil,keterangan FROM karyawan_pelatihan 
		WHERE karyawan_id ={$karyawan_id} 
		AND jadwal_pelatihan_id = {$jadwal_pelatihan_id} ";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		return my_fetch_array($result);
	}
	return false;
}

function update_hasil($karyawan_id , $jadwal_pelatihan_id ){
	$query = "UPDATE karyawan_pelatihan 
		SET hasil='{$_POST['hasil']}' , keterangan ='{$_POST['keterangan']}'
		WHERE karyawan_id ={$karyawan_id} 
		AND jadwal_pelatihan_id = {$jadwal_pelatihan_id} ";
	return  my_query($query);
	 
}

function form_edit_hasil_penilaian($karyawan_id , $jadwal_pelatihan_id){
	$view = form_header( "karyawan_pelatihan" , "karyawan_pelatihan"  );
	
	$query = "SELECT * FROM karyawan_pelatihan 
		WHERE karyawan_id ={$karyawan_id} 
		AND jadwal_pelatihan_id = {$jadwal_pelatihan_id} ";
	$res = my_query($query);
	$fields = my_fetch_array($res);
	
	$jadwal = my_get_data_by_id('jadwal_pelatihan' , 'jadwal_pelatihan_id' ,$jadwal_pelatihan_id );
	$materi = my_get_data_by_id('materi_pelatihan' , 'materi_pelatihan_id' ,$jadwal['materi_pelatihan_id'] );
	$kategori = my_get_data_by_id('kategori_pelatihan' , 'kategori_pelatihan_id' ,$materi['kategori_pelatihan_id'] );
	$view .= form_field_display( $materi['materi_pelatihan_label']   , "Materi pelatihan"   );
	$view .= form_field_display( $jadwal['nama_pelatihan']   , "Nama pelatihan"   );
	
	$hasil_datas = array(
			'lulus'=>'Lulus',
			'gagal'=>'Gagal',
			'absen'=>'Absen'
		);
	$hasil = array(
				'name'=>'hasil',
				'value'=>( isset($_POST['hasil']) ? $_POST['hasil'] : $fields['hasil'] ),
				'id'=>'hasil', 
			);
	$form_field_hasil = form_dropdown($hasil , $hasil_datas);
	
	$view .= form_field_display( $form_field_hasil   , "Hasil penilaian"   );
	
	$keterangan =array(
		'name'=> 'keterangan',
		'id'=> 'keterangan' ,
		'value'=>( isset($_POST['keterangan']) ? $_POST['keterangan'] : $fields['keterangan'] )
	);
	$form_field_keterangan = form_textarea($keterangan); 
	$view .= form_field_display( $form_field_keterangan   , "Keterangan"   );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}